﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ViewDetailOfRequisition : System.Web.UI.Page
    {
        ApproveRequisitionController approveController = new ApproveRequisitionController();
        //String userName = "DeptHead1";
        int empID, reqId;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string reqID = Request.QueryString["reqID"];
                empID = Convert.ToInt32(Request.QueryString["empID"]);
                reqId = Convert.ToInt32(reqID);
                if (!IsPostBack)
                {
                    gvReqDetail.DataSource = approveController.getReqDet(reqId);
                    gvReqDetail.DataBind();

                    lblEmpName.Text = approveController.getEmpNameByEmpID(empID);
                }

                // Block the buttons to prevent multiple clicks by the user
                btnSubmit.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnSubmit).ToString() + ";");
            }
            catch (Exception ex)
            {
                btnSubmit.Enabled = true;            
            }
            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                String deptID = approveController.getDeptID(empID);
                int rowCount = gvReqDetail.Rows.Count;
                List<String> rdoStatus = new List<string>();
                List<String> reasons = new List<string>();

                foreach (GridViewRow r in gvReqDetail.Rows)
                {
                    RadioButton rdoApp = (RadioButton)r.FindControl("rdoApprove");
                    RadioButton rdoRej = (RadioButton)r.FindControl("rdoReject");
                    TextBox txtReason = (TextBox)r.FindControl("txtReason");

                    if (rdoApp.Checked)
                    {
                        rdoStatus.Add("Approve");
                    }
                    else
                    {
                        rdoStatus.Add("Reject");
                    }

                    reasons.Add(txtReason.Text);
                }
                approveController.saveToDeptReq(deptID, reqId, rdoStatus, reasons);
                approveController.SendNotificationToEmployee(empID, deptID);
                btnSubmit.Enabled = true;
                Response.Redirect("~/ApprovalSuccessful.aspx");
            }
            catch
            {
                btnSubmit.Enabled = true;            
            }   
        }

        protected void rdoApprove_CheckedChanged(object sender, EventArgs e)
        {
                
        }

        protected void rdoReject_CheckedChanged(object sender, EventArgs e)
        {

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ApproveRequisition.aspx");
        }
    }
}